<template>
  <div>
    <Form label-position="right" :label-width="400" :model="formData">
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">所属区域：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.districtId" @on-change="changeItem1('fafangWay', fafangWayList, formData.districtId)">
            <Option v-for="item in dataList[0]" :value="item.value" v-bind:key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
        <Col span="5" class="ivu-form-item-label">身份证号码：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.idcardno" placeholder="请输入身份证号码"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">姓名：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.elderName" placeholder="请输入姓名"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">性别：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.sexDictId">
            <Option v-for="item in dataList[4]" :value="item.value" v-bind:key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">民族：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.nationDictId">
            <Option v-for="item in dataList[1]" :value="item.value" v-bind:key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
        <Col span="5" class="ivu-form-item-label">年龄：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.age" placeholder="请输入年龄"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">出生日期：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.birthday" placeholder="请输入出生日期"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">户籍所在地：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.regPlace" placeholder="请输入户籍所在地"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">联系电话：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.mobile" placeholder="请输入联系电话"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">身份证地址：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.idcardnoAddress" placeholder="请输入现住址"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">现住址：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.residentialAddress" placeholder="请输入现住址"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">监护人姓名：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.linkMan" placeholder="请输入监护人姓名"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">监护人联系方式：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.linkManTel" placeholder="请输入监护人联系方式"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">监护人与老人关系：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.relationElderly">
            <Option v-for="item in dataList[2]" :value="item.value" v-bind:key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">监护人地址：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.maintenanceAddress" placeholder="请输入监护人地址"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">监护人身份证号：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.maintenanceIdcardno" placeholder="请输入监护人身份证号码"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">银行卡号：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.bankCardno" placeholder="请输入银行卡号"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">开户行：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.fafangWay" disabled>
            <Option v-for="item in fafangWayList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">代办人姓名：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.agentName" placeholder="请输入代办人姓名"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">代办人身份证号：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.agentIdcardno" placeholder="请输入代办人身份证号"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">代办人住址：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.agentAddress" placeholder="请输入代办人姓名"></Input>
        </Col>
        <Col span="5" class="ivu-form-item-label">代办人联系方式：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Input v-model="formData.agentPhone" placeholder="请输入代办人身份证号"></Input>
        </Col>
      </Row>
      <Row class="mb20">
        <Col span="5" class="ivu-form-item-label">代办人与老人关系：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.agentElderlyRelation">
            <Option v-for="item in dataList[2]" :value="item.value" v-bind:key=" item.value">{{ item.label }}</Option>
          </Select>
        </Col>
        <Col span="5" class="ivu-form-item-label">申请类型：</Col>
        <Col span="6" class="ivu-form-item-content">
          <Select v-model="formData.applicationType">
            <Option v-for="item in dataList[3]" :value="item.value" v-bind:key="item.value">{{ item.label }}</Option>
          </Select>
        </Col>
      </Row>
      <Row class="mb20">
        <Col>
          <Col span="5" class="ivu-form-item-label">备注：</Col>
          <Col span="6" class="ivu-form-item-content">
            <Input v-model="formData.remark" placeholder="请输入备注"></Input>
          </Col>
        </Col>
      </Row>
    </Form>
  </div>
</template>

<script>

import { getdmlbTreeList } from '@/api/eventlist'

export default {
  props: {
    personinfo: Object, // 人员信息
    cardno: String,
    formData2: Object
  },
  name: 'gllrbtForm',
  data () {
    return {
      dataList: [],
      fafangWayList: [],
      applicationTypeList: [],
      formData: { // 上传数据
        age: '',
        agentAddress: '',
        agentElderlyRelation: '',
        agentIdcardno: '',
        agentName: '',
        agentPhone: '',
        bankCardno: '',
        birthday: '',
        cardno: '',
        districtId: '',
        elderName: '',
        fafangWay: '',
        idcardno: '',
        idcardnoAddress: '',
        linkMan: '',
        linkManTel: '',
        maintenanceAddress: '',
        maintenanceIdcardno: '',
        mobile: '',
        nationDictId: '',
        regPlace: '',
        relationElderly: '',
        remark: '',
        residentialAddress: '',
        sexDictId: '',
        applicationType: ''
      }
    }
  },
  watch: {
    formData2 (a) {
      console.log('a', a)
      if (a.idcardno) {
        this.formData = a
      } else if (this.personinfo.card_type === '1') {
        this.formData.elderName = this.personinfo.name // 姓名
        this.formData.idcardno = this.cardno // 身份证
        this.formData.sexDictId = this.cardno.substr(16, 1) % 2 === 1 ? '1' : '2'
        this.formData.birthday = this.cardno.substr(6, 4) + '-' + this.cardno.substr(10, 2) + '-' + this.cardno.substr(12, 2) // 出生日期
        this.formData.age = this.getAge(this.cardno) // 年龄
        this.formData.regPlace = this.personinfo.domicile_address || '' // 户籍地址
        this.formData.idcardnoAddress = this.personinfo.domicile_address || '' // 籍贯
        this.formData.residentialAddress = this.personinfo.address || '' // 现住址
        this.formData.mobile = ''
      } else if (this.personinfo.card_type === '2') {
        this.formData.elderName = this.personinfo.name // 姓名
        this.formData.idcardno = this.personinfo.cardno || '' // 身份证
        this.formData.sexDictId = this.personinfo.sex === '男' ? '1' : '2'
        this.formData.regPlace = this.personinfo.domicile_address || '' // 户籍地址
        this.formData.idcardnoAddress = this.personinfo.domicile_address || '' // 籍贯
        this.formData.residentialAddress = this.personinfo.address || '' // 现住址
      }
    }
  },
  methods: {
    getTree () {
      this.getSelectTree('districtId', 0)
      this.getSelectTree('nationDictId', 1)
      this.getSelectTree('relation_elderly', 2)
      this.getSelectTree('applicationType', 3)
      this.getSelectTree('sexDictId', 4)
    },
    getSelectTree (dmlb, index) {
      getdmlbTreeList({ dmlb }).then(res => {
        if (res.data.errcode === 0) {
          console.log(res, '列表')
          this.dataList[index] = res.data.data
        } else {
          this.$Notice.error({
            title: '获取字典列表失败'
          })
        }
      })
    },
    changeItem1 (dmlb, List, districtId) {
      let param = {}
      param.dmlb = dmlb
      if (['1', '2', '3', '4', '5'].indexOf(districtId) !== -1) {
        param.dmz = '23'
      } else if (['2', '10', '9', '13'].indexOf(districtId) !== -1) {
        param.dmz = '22'
      } else if (['7', '8', '11', '14'].indexOf(districtId) !== -1) {
        param.damz = '7'
      }
      getdmlbTreeList(param).then(res => {
        console.log(param, 'param')
        if (res.data.errcode === 0) {
          this.fafangWayList = res.data.data
          this.formData.fafangWay = this.fafangWayList[0].value
          console.log(this.fafangWayList[0].value, '银行列表')
        } else {
          this.$Notice.error({
            title: '获取字典列表失败'
          })
        }
      })
    },
    getAge (idcardno) {
      let birthday = (idcardno.substr(6, 4) + '-' + idcardno.substr(10, 2) + '-' + idcardno.substr(12, 2)) // 出生日期
      let birthdays = new Date(birthday.replace(/-/g, '/'))
      let d = new Date()
      let age =
        d.getFullYear() -
        birthdays.getFullYear() -
        (d.getMonth() < birthdays.getMonth() ||
        (d.getMonth() === birthdays.getMonth() &&
          d.getDate() < birthdays.getDate()) ? 1 : 0)
      return age
    },
    save () {
      let reg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/
      let reg1 = /^1[3456789]\d{9}$/
      if (reg.test(this.formData.idcardno) === false && this.formData.applicationType === '1') {
        this.$Notice.error({ title: '身份证不合法', desc: '' })
        return false
      } else if (this.formData.idcardno.indexOf('沈字第') === -1 && this.formData.applicationType === '2') {
        this.$Notice.error({ title: '证件不合法', desc: '请用' + '"' + '沈字第' + '"' + '开头的军官证进行办理' })
        return false
      } else if (reg1.test(this.formData.mobile) === false) {
        this.$Notice.error({ title: '联系方式不合法', desc: '' })
        return false
      } else if (this.formData.elderName === '' || this.formData.elderName === undefined) {
        this.$Notice.error({ title: '姓名不合法', desc: '' })
        return false
      } else if (this.formData.sexDictId === '' || this.formData.sexDictId === undefined) {
        this.$Notice.error({ title: '性别不合法', desc: '' })
        return false
      } else if (this.formData.nationDictId === '' || this.formData.nationDictId === undefined) {
        this.$Notice.error({ title: '民族不合法', desc: '' })
        return false
      } else if (this.formData.birthday === '' || this.formData.birthday === undefined) {
        this.$Notice.error({ title: '出生日期不合法', desc: '' })
        return false
      } else if (this.formData.regPlace === '' || this.formData.regPlace === undefined) {
        this.$Notice.error({ title: '户籍所在地不合法', desc: '' })
        return false
      } else if (this.formData.residentialAddress === '' || this.formData.residentialAddress === undefined) {
        this.$Notice.error({ title: '现住址不合法', desc: '' })
        return false
      } else if (this.formData.linkMan === '' || this.formData.linkMan === undefined) {
        this.$Notice.error({ title: '监护人姓名不合法', desc: '' })
        return false
      } else if (reg.test(this.formData.maintenanceIdcardno) === false) {
        this.$Notice.error({ title: '监护人身份信息不合法', desc: '' })
        return false
      } else if (this.formData.maintenanceAddress === '' || this.formData.maintenanceAddress === undefined) {
        this.$Notice.error({ title: '监护人地址不合法', desc: '' })
        return false
      } else if (reg1.test(this.formData.linkManTel) === false) {
        this.$Notice.error({ title: '监护人联系方式不合法', desc: '' })
        return false
      } else if (this.formData.relationElderly === '' || this.formData.relationElderly === undefined) {
        this.$Notice.error({ title: '监护人与老人关系不合法', desc: '' })
        return false
      } else if (this.formData.bankCardno === '' || this.formData.bankCardno === undefined) {
        this.$Notice.error({ title: '银行卡号不合法', desc: '' })
        return false
      } else if (this.formData.fafangWay === '' || this.formData.fafangWay === undefined) {
        this.$Notice.error({ title: '开户行不合法', desc: '' })
        return false
      } else if (this.formData.applicationType === '' || this.formData.applicationType === undefined) {
        this.$Notice.error({ title: '申请类型不合法', desc: '' })
        return false
      } else if (this.formData.districtId === '' || this.formData.districtId === undefined) {
        this.$Notice.error({ title: '所属区域不合法', desc: '' })
        return false
      } else {
        this.$emit('gllrbtForm', this.formData)
        this.$Message.success('表单保存成功')
      }
    },
    savePersonInfo (data) {
      console.log(data, 'data', this.cardno)
      this.personinfo = data
      this.formData.elderName = data.name // 姓名
      this.formData.idcardno = this.cardno || '' // 身份证
      this.formData.sexDictId = data.sex === '男' ? '1' : '2'
      this.formData.regPlace = data.domicile_address || '' // 户籍地址
      this.formData.idcardnoAddress = data.domicile_address || '' // 籍贯
      this.formData.residentialAddress = data.address || '' // 现住址
      this.formData.birthday = this.cardno.substr(6, 4) + '-' + this.cardno.substr(10, 2) + '-' + this.cardno.substr(12, 2) // 出生日期
      this.formData.age = this.getAge(this.cardno) // 年龄
      console.log(this.formData)
    }
  },
  mounted () {
    this.getTree()
    this.changeItem1('fafangWay', this.fafangWayList, this.formData2.districtId)
  }
}
</script>

<style scoped>

</style>
